{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quick Start Tutorial\n", "\n", "Welcome to quick tutorial on FormulaLab, best environment for your formulas database!" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import sympy as sp\n", "\n", "import FormulaLab as fl" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**FormulaLab allows you to:**\n", "\n", " 1. Search for formula(s) in two ways:\n", " 1. Direct search.\n", " \n", " `FormulaLab.FormulaSearch.find(function, [variables,])`\n", " \n", " 2. Derivation: By going through the formulas database and algebraically\n", " solve for the desired formula.\n", " \n", " `FormulaLab.FormulaSearch.derive(function, variable)`\n", " \n", " 2. Convert symbolic formulas into python functions.\n", " \n", " `FormulaLab.FormulaSearch.function(formula:symbolic expr)`\n", " `FormulaLab.FormulaSearch.find(function, [variables,], function=True)`\n", "\n", " 3. Call stored formulas in a database by its ID number, and use it anywhere\n", " in your code with any form.\n", " \n", " `FormulaLab.FormulaSearch.find(function, id)`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Below are four steps of how you may use FormulaLab**\n", "\n", " 1. Import\n", " 2. Initiate\n", " 3. Search\n", " 4. Implement\n", "\n", " \n", "\\* FormulaLab is bult upon [sympy](https://www.sympy.org/en/index.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Importing Formulas from a Database" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Data must ba a pandas.DataFrame, list, dict, tuple, or set**" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['f=m*a', 'a=v/t']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_list = ['f=m*a','a=v/t']\n", "data_dict = {'1':'f=m*a','2':'a=v/t'}\n", "data_dict_2 = {'ID':[1, 2], 'Formula':['f=m*a','a=v/t']}\n", "data_tuple = ('f=m*a','a=v/t')\n", "data_set = {'f=m*a','a=v/t'}\n", "\n", "data_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Data from external Database, should be imported as a DataFrame**\n", " \n", " This is the recommended way of storing and loading formulas, and pandas is \n", " one of the best python packages that can handle this." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IDFormula
01d = v * t
12a = v / t
23f = m * a
35p = m * v
\n", "
" ], "text/plain": [ " ID Formula\n", "0 1 d = v * t\n", "1 2 a = v / t\n", "2 3 f = m * a\n", "3 5 p = m * v" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('Quick Database.csv')\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Initiate the formula search engine" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IDFormulaArgs
01f=m*a[a, m, f]
12a=v/t[v, t, a]
\n", "
" ], "text/plain": [ " ID Formula Args\n", "0 1 f=m*a [a, m, f]\n", "1 2 a=v/t [v, t, a]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phyfos = fl.FormulaSearch(data=data_list) \n", "phyfos.data\n", "# The new \"Args\" column is automatically generated \n", "# to help speed the search algorithm. " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IDFormulaArgs
01f=m*a[a, m, f]
12a=v/t[v, t, a]
\n", "
" ], "text/plain": [ " ID Formula Args\n", "0 1 f=m*a [a, m, f]\n", "1 2 a=v/t [v, t, a]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phyfos = fl.FormulaSearch(data=data_dict) \n", "phyfos.data" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IDFormulaArgs
01d = v * t[v, t, d]
12a = v / t[v, t, a]
23f = m * a[a, m, f]
35p = m * v[p, m, v]
\n", "
" ], "text/plain": [ " ID Formula Args\n", "0 1 d = v * t [v, t, d]\n", "1 2 a = v / t [v, t, a]\n", "2 3 f = m * a [a, m, f]\n", "3 5 p = m * v [p, m, v]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phyfos = fl.FormulaSearch(data=df, formula_col='Formula', id_col='ID') \n", "#formula_col and id_col values should mathch the formulas database\n", "phyfos.data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Search for formula(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**1. Direct search**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[t*v]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = phyfos.find('d') #Searching is case sensitivity\n", "d" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[d/t, a*t, p/m]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = phyfos.find('v')\n", "v" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[a*t]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v_as_a_function_of_a = phyfos.find('v', 'a')\n", "v_as_a_function_of_a" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[v/t]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_as_a_function_of_v = phyfos.find('a', 'v')\n", "a_as_a_function_of_v" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[d/t]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v_as_a_function_of_t_d = phyfos.find('v', ['t','d'])\n", "v_as_a_function_of_t_d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2. Search by formula ID number**\n", " \n", " Each formula has a fixed id number that should not be changed. It \n", " is recomeneded to use this method when you know what formula to use \n", " in your code. It is faster than direct search, and the later could \n", " give you different result when the database is expanded or changed." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[a*m]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = phyfos.find('f', id=3)\n", "f" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[f/a]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Say you want \"m\" to be the function, you do not have to rewrite your formula again!\n", "m = phyfos.find('m', id=3)\n", "m" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[f/m]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Also for 'a'. This is very helpful when there is a mistake \n", "# in the formula! You only change it once in your database.\n", "a = phyfos.find('a', id=3)\n", "a\n", "# You will see how to convert this to a python function in step 4 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2. Deriving formulas, through algabric substitutions**\n", "\n", " This is the most interesting part! FormulaLab can do algebraic \n", " manibulations including integrals and derivatives using the power \n", " of sympy!" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IDFormulaArgs
01d = v * t[v, t, d]
12a = v / t[v, t, a]
23f = m * a[a, m, f]
35p = m * v[p, m, v]
\n", "
" ], "text/plain": [ " ID Formula Args\n", "0 1 d = v * t [v, t, d]\n", "1 2 a = v / t [v, t, a]\n", "2 3 f = m * a [a, m, f]\n", "3 5 p = m * v [p, m, v]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phyfos.data" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phyfos.find('d', 'a') # What if you want to know what is the d(a)??\n", "#It is not in the database! So, find() is not helpful here, derive() is!" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[a*t**2, v**2/a, a*p*t/f]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = phyfos.derive('d', 'a', shortest_path=True) \n", "# shortest_path=True is faster and the default.\n", "d" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 'v', 2], [1, 't', 2], [1, 'v', 5, 'm', 3]]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# You can also see all of your traces:\n", "phyfos.traces" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[a*t**2, v**2/a, a*p*t/f]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = phyfos.derive('d', 'a', shortest_path=False) \n", "d\n", "# Sometimes you get more solutions when you turn off shortest_path, \n", "# because derive() tries with all possible paths, with no shortcuts!" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 'v', 2], [1, 't', 2], [1, 'v', 5, 'm', 3], [1, 't', 2, 'v', 5, 'm', 3]]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# You can also see the all of your traces:\n", "phyfos.traces\n", "# The extra path has a repetitive solution" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{v^{2}}{a}$" ], "text/plain": [ "v**2/a" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# You can pretty print your solutions\n", "# Latex print\n", "d[1]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 2\n", "v \n", "──\n", "a \n" ] } ], "source": [ "sp.pretty_print(d[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4: Use your formulas\n", " Once you get the formula(s) you want, you can use them in many different ways:\n", " 1. Use them as a python functions.\n", " 2. Interact with them in their symbolic form using sympy." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**1. Converting formulas expresion to a python functions**\n", " " ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IDFormulaArgs
01d = v * t[v, t, d]
12a = v / t[v, t, a]
23f = m * a[a, m, f]
35p = m * v[p, m, v]
\n", "
" ], "text/plain": [ " ID Formula Args\n", "0 1 d = v * t [v, t, d]\n", "1 2 a = v / t [v, t, a]\n", "2 3 f = m * a [a, m, f]\n", "3 5 p = m * v [p, m, v]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phyfos.data" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = phyfos.find('f', function=True)\n", "f(m=10, a=2)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = phyfos.find('a',id=3, function=True)\n", "a(f=20, m=10)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle a t^{2}$" ], "text/plain": [ "a*t**2" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# You can also convert symbolic expression to python function\n", "d = phyfos.derive('d', 'a')[0]\n", "d" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d_func = phyfos.function(d)\n", "d_func(t=2, a=1)" ] }, { "cell_type": "markdown", "metadata": { "scrolled": true }, "source": [ "**2. Use your formulas with sympy**\n", " \n", "For more information about how to use sympy, \n", "visit: [SymPy](https://www.sympy.org/en/index.html)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle a t^{2}$" ], "text/plain": [ "a*t**2" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle t^{2} \\sin{\\left(x \\right)}$" ], "text/plain": [ "t**2*sin(x)" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.var('a x')\n", "d = d.subs(a,sp.sin(x))\n", "d" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle t^{2} x - \\frac{t^{2} x^{3}}{6} + \\frac{t^{2} x^{5}}{120} + O\\left(x^{6}\\right)$" ], "text/plain": [ "t**2*x - t**2*x**3/6 + t**2*x**5/120 + O(x**6)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.series(x)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle t^{2} \\cos{\\left(x \\right)}$" ], "text/plain": [ "t**2*cos(x)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.diff(x) # take the derivative of d with respect to x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Else can be done with FormulaLab" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d = v * t'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Find formula as string, by its id number\n", "str_fo = phyfos.find_raw_formula(1) \n", "str_fo" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[d/t]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Convert str expr to formula and solve for a variable\n", "phyfos.solve_for(expr=str_fo, var='v')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 5]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Tels you where to find certain variable in your database\n", "phyfos.get_formula_ids('v')" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phyfos.get_formula_ids(('v','a')) # --> 'v' and 'a' can be found in id = 2" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[5, 'v', 1, 'v', 2], [5, 'v', 1, 't', 2]]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# What if you want to know how your formulas are connected!\n", "phyfos.trace([5,1,2])" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{a*p*t/f, a*t**2, v**2/a}" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Once you are done, you can see all your derived formulas stored in\n", "phyfos.all_derived_formulas" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }